package com.wk1un2.config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.wk1un2.entity.Users;
import com.wk1un2.service.UsersService;

public class CustomRealm extends AuthorizingRealm {

	@Autowired
	private UsersService userInfoService;

	/** 
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		System.out.println("执行了授权");
		SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
		return simpleAuthorizationInfo;
	}

	/**
	 * 认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
			throws AuthenticationException {
		System.out.println("执行了认证");

		String username = (String) authenticationToken.getPrincipal();

		Users user = userInfoService.getByUsersName(username);

		if (user == null) {
			
			return null;
		} else {
			SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user, user.getUserpassword(),
					getName());
			System.out.println("users!=null" + simpleAuthenticationInfo);
			return simpleAuthenticationInfo;
		}
	}
}